from django.db import models
from django.contrib.localflavor.us.models import PhoneNumberField
import datetime

STATUS = [
        (u'Single', u'Single'), 
        (u'Married', u'Married'),
        (u'Divorced', u'Divorced'), 
        (u'Widow', u'Widow'),
        (u'Widower', u'Widower')
        ]
PROFESSION_LIST = [
                   (u'Armed Forces', u'Armed Forces'),
                   (u'Banker', u'Banker'),
                   (u'Businessman', u'Businessman'),
                   (u'Doctor', u'Doctor'),
                   (u'Engineer', u'Engineer'),
                   (u'Govt. Employee', u'Govt. Employee'),
                   (u'House Wife', u'House Wife'),                   
                   (u'Lawyer', u'Lawyer'),
                   (u'Police', u'Police'),
                   (u'Private Employee', u'Private Employee'),
                   (u'Retired', u'Retired'),
                   (u'Services', u'Services'),
                   (u'Shop Keeper', u'Shop Keeper'),
                   (u'Teacher', u'Teacher'),
                   (u'Unemployed', u'Unemployed'),
                   (u'Other', u'Other...'),
                   ]
EDUCATION_LIST = [
                  (u'Primary', u'Primary'),
                  (u'Secondary', u'Secondary'),
                  (u'Matriculation', u'Matriculation'),
                  (u'O Level', u'O Level'),
                  (u'Intermediate', u'Intermediate'),
                  (u'A Level', u'A Level'),
                  (u'Bachelor', u'Bachelor'),
                  (u'Master', u'Master'),
                  (u'Higher Studies', u'Higher Studies')
                  ]
KABILAH_LIST = [
                (u'Chananna', u'Chananna'),
                (u'Chawwla', u'Chawwla'),
                (u'Chhokra', u'Chhokra'),
                (u'Dhingrah', u'Dhingrah'),
                (u'Dhograyja', u'Dhograyja'),
                (u'Jawa', u'Jawa'),
                (u'KhudaBanda', u'KhudaBanda'),
                (u'Khurana', u'Khurana'),
                (u'Kukrayja', u'Kukrayja'),
                (u'Sachdev', u'Sachdev'),
                (u'Wayyar', u'Wayyar'),
                (u'Other', u'Other...'),
                ]
GENDER_CHOICES = [
                  (u'M', u'Male'),
                  (u'F', u'Female'),
                  ]

class Family(models.Model):
    BLOOD_GROUP = [
            (u'A+', u'A+'),
            (u'A-', u'A-'),
            (u'B+', u'B+'),
            (u'B-', u'B-'),
            (u'AB+', u'AB+'),
            (u'AB-', u'AB-'),
            (u'O+', u'O+'),
            (u'O-', u'O-')
            ]
    HOUSE_OWNERSHIP = [
                       (True, u'Owned'),
                       (False, u'Rented')
                       ]
    CHOICE = [
              (True, u'Yes'),
              (False, u'No')
             ]
    family_no = models.AutoField(verbose_name = 'Family No.', primary_key=True, editable=False)
    form_no = models.CharField(max_length=10, verbose_name='Form No.')
    name = models.CharField(max_length=80, verbose_name='Full Name')
    father_name = models.CharField(verbose_name = 'Father/Husband Name', max_length=60)
    sex  = models.CharField(max_length=2, choices=GENDER_CHOICES, default = GENDER_CHOICES[0][0], verbose_name='Gender')
    known_as = models.ForeignKey('FamilyName', verbose_name='Almaarof')
    caste = models.CharField(max_length = 10, verbose_name='Kabilah', choices = KABILAH_LIST)    
    profession = models.CharField(max_length=17, choices= PROFESSION_LIST)
    marital_status = models.CharField(verbose_name='Marital Status',max_length=10, choices = STATUS)
    dob = models.DateField(verbose_name = 'Date of Birth', blank=True)
    birth_place = models.CharField(verbose_name='Birth Place', max_length=30, blank=True)
    education = models.CharField(max_length=15, choices=EDUCATION_LIST)
    blood_group = models.CharField(verbose_name = 'Blood Group',max_length=4, choices = BLOOD_GROUP, blank=True)
    id_no = models.CharField(verbose_name='CNIC No.', max_length=15, unique=True)
    mobile_no = models.CharField(verbose_name='Mobile No.', max_length=20)
    email = models.EmailField(max_length=40, blank=True)
    # Residence Address
    h_owenership = models.BooleanField(verbose_name='House Owned/Rented', choices = HOUSE_OWNERSHIP, default=True)
    h_no = models.CharField(max_length=10, verbose_name='House No.')
    h_block = models.CharField(max_length=20, verbose_name='Block/Street/Mohalla')
    h_area = models.CharField(max_length=25, verbose_name='Area/Society')
    h_city = models.CharField(max_length=25, verbose_name='City', default='Lahore')
    h_country = models.CharField(verbose_name = 'Country', max_length=20, default='Pakistan')
    h_phone_no = models.CharField(max_length=40, blank=True, verbose_name='Phone Number(s)')
    # Business Address
    business_name = models.CharField(verbose_name='Office/Shop/Organization Name', max_length=30, blank=True)
    business_addr = models.TextField(verbose_name='Address', max_length = 120, blank=True)
    b_phone_no = models.CharField(max_length=40, blank=True, verbose_name='Phone Number(s)')
    # Affiliation
    member_hussainia = models.BooleanField(verbose_name='Member Anjuman Hussainiya', choices = CHOICE, default=False)
    member_hussainia_detail = models.TextField(verbose_name='Membership Details', max_length=150, blank=True)
    member_other = models.BooleanField(verbose_name='Member Other Organization', choices = CHOICE, default=False)
    member_of = models.CharField(verbose_name='Organization Name', max_length = 50 , blank=True)
    member_other_detail = models.TextField(verbose_name='Membership Details', max_length=150,blank=True)
    # Donation
    donar_anjuman = models.BooleanField(verbose_name='Anjuman Hussainia Charity Donar', choices = CHOICE, default=False)
    anjuman_donar_receipt = models.CharField(verbose_name='Receipt Name', max_length = 25, blank=True)
    donar_family_welfare = models.BooleanField(verbose_name='Family Welfare Fund', choices = CHOICE, default=True)
    family_welfare_receipt = models.CharField(verbose_name='Receipt Name', max_length = 25, blank=True)
    # Voting
    registered_voter = models.BooleanField(verbose_name='Registered Voter', choices = CHOICE, default=True)
    voting_area = models.CharField(verbose_name='Electoral Area/U.C No.', max_length = 10, blank=True)
    # Office Use
    entered_by = models.CharField(max_length=80, verbose_name='Form Entered By')
    entree_mobile_no = models.CharField(verbose_name='Mobile No.', max_length=20)
    entry_date = models.DateField(verbose_name = 'Entry Date', blank=True)
    posted_by = models.CharField(max_length=80, verbose_name='Form Posted By')
    poster_mobile_no = models.CharField(verbose_name='Mobile No.', max_length=20)
    posting_date = models.DateField(verbose_name = 'Posting Date', blank=True)
    incharge = models.CharField(max_length=80, verbose_name='Kabilah Incharge Name')
    incharge_mobile_no = models.CharField(verbose_name='Mobile No.', max_length=20)
    check_date = models.DateField(verbose_name = 'Date', blank=True)
    
    
    def __unicode__ (self):
        return "{0}".format(self.name)
    class Meta:
        verbose_name = "Family"
        verbose_name_plural = 'Families'
    def head_count(self):
        try:
            adults = AdultDependant.objects.filter(guardian=self).count()
            childs = ChildDependant.objects.filter(guardian=self).count()
            return (adults + childs)
        except:
            raise
    def dependent_male(self):
        try:
            male = []
            male = AdultDependant.objects.filter(guardian=self, sex='M')
        except:
            pass
        finally:
            return male
        
    def dependent_female(self):
        try:
            female = []
            female = AdultDependant.objects.filter(guardian=self, sex='F')
        except:
            pass
        finally:
            return female
    def dependent_boys(self):
        try:
            boys = []
            boys = ChildDependant.objects.filter(guardian=self, sex='M')
        except:
            pass
        finally:
            return boys
    def dependent_girls(self):
        try:
            girls = []
            girls = ChildDependant.objects.filter(guardian=self, sex='F')
        except:
            pass
        finally:
            return girls
    
class AdultDependant(models.Model):
    name = models.CharField(max_length=80)
    relation_w_guardian = models.CharField(max_length=20, verbose_name='Relation with Guardian')
    sex  = models.CharField(max_length=2, choices=GENDER_CHOICES)
    marital_status = models.CharField(verbose_name='Marital Status',max_length=10, choices = STATUS)
    dob = models.DateField(verbose_name = 'Date of Birth', blank=True)
    education = models.CharField(max_length=15, choices=EDUCATION_LIST)
    profession = models.CharField(max_length=17, choices= PROFESSION_LIST)
    id_no = models.CharField(verbose_name='CNIC No.', max_length=15, unique=True)    
    guardian = models.ForeignKey('Family')
    
    def __unicode__ (self):
        return "{0}".format(self.name)
    class Meta:
        verbose_name = "Adult Dependant"
        verbose_name_plural = 'Adult Dependants'
    def caste(self):
        try:
            guardian = Family.objects.get(family_no=self.guardian.family_no)
            return guardian.caste
        except:
            raise
    
class ChildDependant(models.Model):
    name = models.CharField(max_length=80)
    relation_w_guardian = models.CharField(max_length=20, verbose_name='Relation with Guardian')
    sex  = models.CharField(max_length=2, choices=GENDER_CHOICES)
    dob = models.DateField(verbose_name = 'Date of Birth', blank=True)
    education = models.CharField(max_length=15, choices=EDUCATION_LIST)
    id_no = models.CharField(verbose_name='B-Form No.', max_length=15, unique=True)    
    guardian = models.ForeignKey('Family')
    
    def __unicode__ (self):
        return "{0}".format(self.name)
    class Meta:
        verbose_name = "Child Dependant"
        verbose_name_plural = 'Child Dependants'
        
    def caste(self):
        try:
            guardian = Family.objects.get(family_no=self.guardian.family_no)
            return guardian.caste
        except:
            raise
    
class FamilyName(models.Model):
    alle = models.CharField(verbose_name = 'Alle/Almaroof', max_length=30)
    caste = models.CharField(max_length = 15, verbose_name='Kabilah', choices = KABILAH_LIST)
    
    def __unicode__ (self):
        return "{0}".format(self.alle)
    
    class Meta:
        verbose_name = "Kabilah / Alle" 
        verbose_name_plural = "Kabilah / Alle"
